Flag: Tornado! Hurricane!

Blogs >> Sirmabus's Blog

Created: Sunday, November 25 2007 02:58.49 CST Modified: Tuesday, November 27 2007 17:51.35 CST
Printer Friendly ...
Intrinsic "_ReturnAddress()" C/C++ WTF!
Author: Sirmabus # Views: 6211

While browsing around I ran into an article that showed a few intrinsics that might be of interested to those using API hooks, etc.  Probably been around since at least VS2003,  but I just learned of them.  I guess I have to read MSDN, etc., more often.


void *_ReturnAddress(void)


There is also a "_AddressOfReturnAddress()" which might just be a clean and handy way to look at a functions arguments on the stack along with the return address.

Return address, and, or, better the calling stack are handy things to know when you do API hooks for reversing, and advanced monitoring.
You can track particular API usage right down to the code where invoked, etc.


In the past I resorted to strange hacks using inline assembly like this:


DWORD dwReturn = -1;
_asm
{  
                //int 3
mov  eax,[esp + (4 * 4)]
mov  dwReturn,eax
};


Ugly huh? Easily broken and error prone.  You have to figure out the offset from where the stack is manually.  I'd stick that "int 3" in there and catch it in a debugger to figure out the stack offset needed.

Also one can directly use in an API hook (or as a stub) a "__declspec(naked)" declared function with more inline asm,
but this is hardly ideal too.


To use these intrinsics just add the header file:
#include <intrin.h>

And this should give you the return address of the function your in:
PVOID pMyReturn = _ReturnAddress();


EDIT: Tested it out in some normal wrapper/sub-class hooks and works perfectly.








Blog Comments
neox Posted: Sunday, November 25 2007 09:38.41 CST
Nice. thanks.

c1de0x Posted: Monday, November 26 2007 01:30.19 CST
Anyone know how this works? Does the compiler treat these intrinsics differently or does it just contain an EBP-relative lookup?

Sirmabus Posted: Monday, November 26 2007 06:00.54 CST
I tried it and it works great.

I'm sure it's something in the compiler that just figures out the right offset to the stack for you. Be it realative to EBP or directly to ESP, etc.


EliCZ Posted: Monday, November 26 2007 10:23.14 CST
"More universal" example (non-RISC platforms):

#include <stdio.h>
void *f(void *p0, ...) {
  return(*(void**)((size_t)&p0-sizeof(void*)));
}
int main(void) {
  return(printf("%p\n", f(0)));
}

PSUJobu Posted: Wednesday, November 28 2007 11:45.05 CST
Ironically I just stumbled across the GCC equivalent yesterday:  __builtin_return_address().  Look up the NET_CALLER macro in your favorite LXR.

anonymouse Posted: Wednesday, November 28 2007 11:49.56 CST
and ms has documentation on this intrinsics


// compiler_intrinsics_AddressOfReturnAddress.cpp
#include <stdio.h>
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

// _ReturnAddress and _AddressOfReturnAddress should be prototyped before use
EXTERNC void * _AddressOfReturnAddress(void);
EXTERNC void * _ReturnAddress(void);

#pragma intrinsic(_AddressOfReturnAddress)
#pragma intrinsic(_ReturnAddress)

/*
* This function will print three values:
*   (1) The address retrieved from _AddressOfReturnAdress
*   (2) The return address stored at the location returned in (1)
*   (3) The return address retrieved the _ReturnAddress* intrinsic
*
* Note that (2) and (3) should be the same address.
*/
__declspec(noinline)
void func(void)
{
   void* pvAddressOfReturnAddress = _AddressOfReturnAddress();
   printf("%p\n", pvAddressOfReturnAddress);
   printf("%p\n", *((void**) pvAddressOfReturnAddress));
   printf("%p\n", _ReturnAddress());
}

int main(void)
{
   func();
   return 0;
}


http://msdn2.microsoft.com/en-us/library/s975zw7k(VS.71).aspx



Add New Comment
Comment:









There are 31,316 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
hi!
Jul/01
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit